<?php
require_once './source/class/class_core.php';
$discuz = & discuz_core::instance();
$discuz->init();
/*
if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1'){
	$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
	header("Location: $url");
}
*/
$prov = '';
if(isset($_GET['prov'])){
	$prov = strval($_GET['prov']);
}

if(!$_G['uid'] || !in_array($prov, array('gmail', 'yahoo'))) {
	header("Location: http://". $_SERVER['SERVER_NAME']);
}

include('OI/inviter.php');
$inviter=new OpenInviter();
$oi_services=$inviter->getPlugins();
if (isset($_POST['provider_box'])) {
	$providerIMG = $_POST['provider_box'];
	if (isset($oi_services['email'][$_POST['provider_box']])) $plugType='email';
	elseif (isset($oi_services['social'][$_POST['provider_box']])) $plugType='social';
	else $plugType='';
}
else $plugType = '';


function ers($ers){
	if (!empty($ers)){
		$contents="<br /><table cellspacing='0' cellpadding='0' style='border:1px solid red; width=100%;' align='center'><tr><td valign='middle' style='color:red;padding:5px;'>";
		foreach ($ers as $key=>$error)
			$contents.="- {$error}<br >";
		$contents.="</td></tr></table>";
		return $contents;
	}
}
	
function oks($oks){
	if (!empty($oks)){
		$contents="<table border='0' cellspacing='0' cellpadding='10' style='border:1px solid #5897FE;' align='center'><tr><td valign='middle' style='color:#5897FE;padding:5px;'>	";
		foreach ($oks as $key=>$msg)
			$contents.="{$msg}<br >";
		$contents.="</td></tr></table><br >";
		return $contents;
	}
}

if (!empty($_POST['step'])) $step=$_POST['step'];
else $step='get_contacts';

$ers=array();$oks=array();$import_ok=false;$done=false;
if ($_SERVER['REQUEST_METHOD']=='POST'){
	if ($step=='get_contacts'){
		if (empty($_POST['email_box']))
			$ers['email']="Báº¡n chÆ°a nháº­p vÃ o Ä‘á»‹a chá»‰ Email!";
		if (empty($_POST['password_box']))
			$ers['password']="Báº¡n chÆ°a nháº­p vÃ o Máº­t Kháº©u!";
		if (empty($_POST['provider_box']))
			$ers['provider']="Báº¡n chÆ°a lá»±a chá»�n Provider!";
		if (count($ers)==0){
			$inviter->startPlugin($_POST['provider_box']);
			$internal=$inviter->getInternalError();
			if ($internal)
				$ers['inviter']=$internal;
			elseif (!$inviter->login($_POST['email_box'],$_POST['password_box'])){
				$internal=$inviter->getInternalError();
				$ers['login']=($internal?$internal:"KhÃ´ng thá»ƒ Ä‘Äƒng nháº­p, Vui lÃ²ng kiá»ƒm tra láº¡i Ä‘á»‹a chá»‰ Email vÃ  Máº­t kháº©u !");
			}
			elseif (false===$contacts=$inviter->getMyContacts())
				$ers['contacts']="KhÃ´ng thá»ƒ láº¥y Ä‘Æ°á»£c danh báº¡ liÃªn láº¡c vÃ o lÃºc nÃ y, xin vui lÃ²ng thá»­ láº¡i sau!";
			else{
				$import_ok=true;
				$step='send_invites';
				$_POST['oi_session_id']=$inviter->plugin->getSessionID();
				$_POST['message_box']='';
			}
		}
	}elseif ($step=='send_invites'){
		if (empty($_POST['provider_box'])) $ers['provider']='Báº¡n chÆ°a lá»±a chá»�n Provider!';
		else{
			$inviter->startPlugin($_POST['provider_box']);
			$internal=$inviter->getInternalError();
			if ($internal) $ers['internal']=$internal;
			$done=true;
		}
	}
}else{
	$_POST['email_box']='';
	$_POST['password_box']='';
	$_POST['provider_box']='';
}

$contents='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="static/OI/css/css.css">
<script type="text/javascript">
	function click_submit(){
		document.getElementById("popup_document").style.display = "";
		var sTop = document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		document.getElementById("popup_document").style.top = sTop + "px";
	}
</script>
';
if (!$done){
	// Thiáº¿t káº¿ giao diá»‡n cho form Ä‘Äƒng nháº­p
	if ($step=='get_contacts'){
		if($prov == 'gmail'){
			$contents.='<title>Ä�Äƒng nháº­p Google Mail</title>
						</head>
						<body>
							<form action="" method="POST" name="frmGetContact" id="frmGetContact">
							<div class="body-ya body-gm">
							  <div id="wrap-ya">
						       	<div class="header-ya header-gm"><img src="static/OI/images/logo_google.png" /></div>
						       	  <div class="header-content-ya clearfix">
						           	  <div class="woo-left clearfix">
						                  <div class="box-woo-left">
						                      <div class="lable-woo-ya">Má»�i báº¡n bÃ¨</div>
						                      <div class="content-woo-ya">
						                      	<p>Má»�i báº¡n bÃ¨ tham gia Máº¡ng xÃ£ há»™i Viá»‡t Nam WOO.VN qua danh báº¡ Gmail cá»§a báº¡n.</p>
						                      	<p>Ä�á»ƒ láº¥y Ä‘Æ°á»£c danh báº¡, xin vui lÃ²ng Ä‘Äƒng nháº­p vÃ o Gmail trÆ°á»›c.</p>
						                      	<p>WOO cam káº¿t khÃ´ng láº¥y thÃªm thÃ´ng tin nÃ o ngoÃ i danh báº¡ cá»§a báº¡n, khÃ´ng lÆ°u giá»¯ hay cung cáº¥p cho bÃªn thá»© ba báº¥t kÃ¬ thÃ´ng tin gÃ¬ vá»� tÃ i khoáº£n cá»§a báº¡n!</p>
						                      	<div>' .ers($ers).'</div>
						                      	</div>
						                  </div>
						              </div>
						           	  <div class="woo-right woo-right-gm clearfix">
						                  <div class="icon-logo-google"></div>
						               	  <div class="lable-dangnhap-ya">Ä�Äƒng nháº­p!</div>
						               	  <div class="name-pass">Email:</div>
						                  <div class="ya-input"><input  type="text" name="email_box" id="email_box" value="" /></div>
						               	  <div class="name-pass">Máº­t kháº©u:</div>
						                  <div class="ya-input"><input  type="password" name="password_box" value="" />
						                  	<input type="hidden" name="step" value="get_contacts">
						                  </div>
						                  <div class="bottom-ya bottom-gm"><input type="submit" value="Ä�Äƒng nháº­p" onclick="click_submit();" /></div>
						              </div>
						          </div>
						        </div>    
						    </div>';
		}elseif($prov == 'yahoo'){
			$contents.='<title>Ä�Äƒng nháº­p Yahoo! Danh báº¡</title>
						</head>
						<body>
							<form action="" method="POST" name="frmGetContact" id="frmGetContact">
							<div class="body-ya">
								<div id="wrap-ya">
						        	<div class="header-ya"><img src="static/OI/images/logo-yahoo.png" /></div>
						        	<div class="header-content-ya clearfix">
						            	<div class="woo-left clearfix">
						                	<div class="box-woo-left">
						                        <div class="lable-woo-ya">Má»�i báº¡n bÃ¨</div>
						                        <div class="content-woo-ya">
						                            <p>Má»�i báº¡n bÃ¨ tham gia Máº¡ng xÃ£ há»™i Viá»‡t Nam WOO.VN qua Yahoo! Danh Báº¡ cá»§a báº¡n.</p>
						                            <p>Ä�á»ƒ láº¥y Ä‘Æ°á»£c danh báº¡, xin vui lÃ²ng Ä‘Äƒng nháº­p vÃ o Yahoo! Danh Báº¡ trÆ°á»›c.</p>
						                            <p>WOO cam káº¿t khÃ´ng láº¥y thÃªm thÃ´ng tin nÃ o ngoÃ i danh báº¡ cá»§a báº¡n, khÃ´ng lÆ°u giá»¯ hay cung cáº¥p cho bÃªn thá»© ba báº¥t kÃ¬ thÃ´ng tin gÃ¬ vá»� tÃ i khoáº£n cá»§a báº¡n!</p>
						                            <div>' .ers($ers).'</div>
						                        </div>
						                	</div>
						                </div>
						            	<div class="woo-right clearfix">
						                	<div class="lable-dangnhap-ya">Ä�Äƒng nháº­p Yahoo!</div>
						                	<div class="name-pass">Email:</div>
						                    <div class="ya-input"><input  type="text" name="email_box" id="email_box" value="" /></div>
						                	<div class="name-pass">Máº­t kháº©u:</div>
						                    <div class="ya-input">
						                    	<input  type="password" name="password_box" value="" />
						                    	<input type="hidden" name="step" value="get_contacts">
						                    </div>
						                    <div class="bottom-ya">
						                    	<input type="submit" value="Ä�Äƒng nháº­p" onclick="click_submit();"/>
						                    </div>
						                    
						                </div>
						            </div>
						        </div>    
						    </div>';
		}
		
	// Láº¥y dá»¯ liá»‡u Ä‘á»ƒ chÃ¨n vÃ o pháº§n ngÆ°á»�i nháº­n mail
	}elseif($step=='send_invites'){
		if ($inviter->showContacts()){
			$contents.="<title>Nháº­p danh báº¡</title>
						</head>
						<body>
						<form action='' method='POST' name='frmGetContact' id='frmGetContact'>
						<div id='contentContact' style='max-height:300px; overflow-y: auto;overflow-x:hidden; width:100%'>";
			if (count($contacts)==0){
				$contents.="\n\t<div>Báº¡n khÃ´ng cÃ³ liÃªn láº¡c nÃ o trong danh báº¡ nÃ y.</div>";
				$contents .= '
					<script type="text/javascript"> 
						window.opener.document.getElementById("divElement").innerHTML = document.getElementById("frmGetContact").innerHTML;
						window.opener.document.getElementById("email").type = "text";
						//this.window.close();
					</script>';
			}else{
				$counter=0;
				foreach ($contacts as $email=>$name){
					
					DB::query("REPLACE ".DB::table('common_member_frq_other')." SET `uid` = '$_G[uid]', `name` = '$name', `email` = '$email'");
					
					$counter++;
					if ($providerIMG == 'yahoo'){
						@list($yids, $exten) = @split('@', $email);
						$IMGavatar = "<div class='child_2_1'><img src='http://img.msg.yahoo.com/avatar.php?yids=$yids' width='45px' height='45px' /></div>";
					}else{
						$IMGavatar = '';
					}
					$contents.="
					<div class='parent_1'>
				    	<div class='child_1'>
				        	<input name='check_{$counter}' value='$email' type='checkbox' checked>&nbsp;
				        </div>
				        <div class='child_2'>
				        	$IMGavatar
				            <div class='child_2_2'>
				                <div class='child_2_2_1'>{$name}</div>
                				<div class='child_2_2_2'>{$email}</div>
				            </div>
				        </div>
				        <div style=clear:both;></div>
				    </div>
					";
				}
			}
			$contents.="</div><label for='toggle_all' ><input type='checkbox' onChange='toggleAll(this)' name='toggle_all' id='toggle_all' title='Chá»�n/Bá»� chá»�n táº¥t cáº£' checked>Chá»�n/Bá»� chá»�n táº¥t cáº£</label>";
			$contents .= '
			<script type="text/javascript"> 
				window.opener.document.getElementById("divElement").innerHTML = document.getElementById("frmGetContact").innerHTML;
				window.opener.document.getElementById("email").type = "hidden";
				this.window.close();
			</script>';
		}
	}
}
$contents.="<input type='hidden' name = 'provider_box' value = '$prov' /></form>
<div id='frt' style='padding:5px'>
<p>Copyright Â© 2011 <a href='http://www.woo.vn' title='woo...'>woo.vn</a> - All rights reserved.</p>
            <p style='font-size:10px'>Má»™t sáº£n pháº©m cá»§a IACD.,JSC</p>
</div>
	<div id='popup_document' style='display:none;text-align:center; background: transparent; background: rgba(0, 0, 0, 0.8); filter:progid:DXImageTransform.Microsoft.gradient(startColorstr = #80000000, endColorstr = #80000000); position:absolute; margin:auto; z-index:9999; width:100%; height:100%; overflow:hidden; '>
    	<div id='loading_load' style='height:100%; width:100%; line-height:700px; margin:auto;text-align:center;'>
        	<img src='static/image/common/loading_page.gif' />&nbsp;
        </div>
    </div>
</body>
</html>";
echo $contents;
?>